package com.maserb.monitor.dataCenter.communication;

import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@Data
public class SystemTime {
    static SimpleDateFormat timeFormat = new SimpleDateFormat("mm:ss");
    static SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    private short year;
    private short month;
    private short dayOfWeek;
    private short day;
    private short hour;
    private short minute;
    private short second;
    private short milliseconds;

    public static SystemTime convert(byte[] content, Box<Integer> index) {
        SystemTime st = new SystemTime();
        st.year = ByteUtils.getShort(content, index);
        st.month = ByteUtils.getShort(content, index);
        st.dayOfWeek = ByteUtils.getShort(content, index);
        st.day = ByteUtils.getShort(content, index);
        st.hour = ByteUtils.getShort(content, index);
        st.minute = ByteUtils.getShort(content, index);
        st.second = ByteUtils.getShort(content, index);
        st.milliseconds = ByteUtils.getShort(content, index);
        return st;
    }

    public Date toDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day, hour, minute, second);
        calendar.add(Calendar.MILLISECOND, milliseconds);
        return calendar.getTime();
    }

    public String toDateTime() {
        return datetimeFormat.format(toDate());
    }

    public String toTime() {
        return timeFormat.format(toDate());
    }
}
